package com.example.liuxueqin.mybluetoothdemo.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.liuxueqin.mybluetoothdemo.MainActivity;



public class BlueToothUtil {

    public static BlueToothUtil blueToothUtilInstance;
    private Context mContext;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothDevice device;
    private final String tag = "zhangphil";
    private final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
    private static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 1;
    private int REQUEST_ENABLE_BT = 0xa01;


    private BlueToothUtil(Context context) {
        mContext = context;
        if (context == null) {
            throw new RuntimeException("Parameter context can not be null !");
        }
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }


    //单例模式 获取bluethoothutil 的实例 不用每次都再去new
    public static BlueToothUtil getBlueToothUtilInstance(Context context) {
        if (blueToothUtilInstance == null) {
            blueToothUtilInstance = new BlueToothUtil(context);
        }
        return blueToothUtilInstance;
    }


    //判断该设备是否支持蓝牙和该设备蓝牙是否已经打开
    public void blueIsOpen(MainActivity mainActivity) {

        if (mBluetoothAdapter != null) {
            if (mainActivity != null && !mBluetoothAdapter.isEnabled()) {
                //没开启就需要打开蓝牙
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                mainActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            } else {
                throw new RuntimeException("Parameter context can not be null !");
            }
        }else {
            //说明设备不支持蓝牙 直接吐司一个该设备不支持蓝牙然后关闭当前activit
            Toast.makeText(mainActivity,"您的设备不支持蓝牙",Toast.LENGTH_LONG).show();


        }


    }

}
